package com.ilop.sthome.ui.activity.device;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.DeviceShareBean;
import com.ilop.sthome.mvp.contract.message.ShareContract;
import com.ilop.sthome.mvp.present.message.SharePresenter;
import com.ilop.sthome.ui.adapter.device.DeviceShareAdapter;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDeviceShareBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author skygge
 * @date 2019/9/27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备分享页面
 */

public class DeviceShareActivity extends ContainLoginActivity<SharePresenter, ActivityDeviceShareBinding> implements ShareContract.IView {

    private DeviceShareAdapter mAdapter;
    private String mIotId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_share;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mIotId = getIntent().getStringExtra(CommonId.KEY_IOT_ID);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new SharePresenter(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new DeviceShareAdapter(mContext);
        mDBind.shareList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        LiveDataBus.get().with(CommonId.SHARE_CANCEL, String.class).observe(this, s -> mPresent.cancelShare(s));
        LiveDataBus.get().with(CommonId.SHARE_CONFIRM, String.class).observe(this, s -> mPresent.confirmShare(s));
        LiveDataBus.get().with(CommonId.SHARE_DELETE, DeviceShareBean.class).observe(this, share -> {
            assert share != null;
            mPresent.onConfirmUnbind(share.getTargetId(), share.getReceiverAlias());
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivShareBack.setOnClickListener(view -> finish());
        mDBind.tvShareAdd.setOnClickListener(v -> mPresent.addSharer(mIotId));
        mDBind.tvClearSharer.setOnClickListener(v -> mPresent.clearShareNoticeList());
        mDBind.shareList.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresent.getShareNoticeList();
                mDBind.shareList.complete();
            }

            @Override
            public void onLoadMore() {
                mDBind.shareList.complete();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresent.getShareNoticeList();
    }

    @Override
    public void showShareList(List<DeviceShareBean> shareList) {
        List<DeviceShareBean> shareBeanList = new ArrayList<>();
        for (DeviceShareBean share:shareList) {
            if (share.getIsReceiver() == 0 && mIotId.equals(share.getTargetId())){
                shareBeanList.add(share);
            }
        }
        if (shareBeanList.size()>0){
            mAdapter.setList(shareBeanList);
        }else {
            showNoSharer();
        }
    }

    @Override
    public void showNoSharer() {
        mAdapter.setList(null);
        mDBind.shareList.setEmptyView(getString(R.string.no_sharer), R.mipmap.sharelist_empty);
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void dismissProgress() {
        dismissProgressDialog();
    }
}
